<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="u-box">
					<text v-if="item.isDefault === 1" class="tag">默认</text>
					<text class="name">{{item.realName}}</text>
					<text class="mobile">{{item.phone}}</text>
				</view>
				<view class="address-box">
					<view>
						<view class="address">{{item.addressAlias}}{{item.address}}</view>
					</view>
				</view>
			</view>
			<text class="iconfont iconbianji" @click.stop="addAddress('edit', item)"></text>
		</view>

		<button class="zy-add-btn" @click="addAddress('add')">新增地址</button>

		<view class="uni-loadmore" v-if="showLoadMore">{{loadMoreText}}</view>
	</view>
</template>

<script>
	import {
		doPostJson,
		doGet,
		nullToStr,
		showInfoToast,
		showSuccessToast
	} from '@/common/util.js'
	import * as ResponseStatus from '@/common/response-status.js'
	export default {
		data() {
			return {
				loadMoreText: "加载中...",
				showLoadMore: false,
				source: 0, // 来源，如果是1，则表示是从下单页面过来的
				addressList: [],
				pager: {
					pageNo: 1,
					pageSize: 10,
					isActive: 0
				},
				urls: {
					listUrl: '/shipping-address/user/pager-cond',
				}
			}
		},
		onLoad(option) {
			this.source = option.source;
			this.loadData('init');
		},
		onPullDownRefresh() {
			this.pager.pageNo = 1;
			this.loadData('pullDown');
		},
		onReachBottom() {
			this.pager.pageNo += 1;
			this.loadData('reachBottom');
		},
		methods: {
			/**
			 * 加载数据
			 */
			loadData(type) {
				this.loadAddressList(type)
			},
			/**
			 * 加载地址列表数据
			 */
			loadAddressList(type) {
				uni.showLoading({
					title: '加载中'
				})
				doPostJson(this.urls.listUrl, this.pager, {}, true).then(response => {
					let [error, res] = response;
					if (res.data.code === ResponseStatus.OK) {
						const rows = nullToStr(res.data.data.rows);
						if (type === 'init') {
							this.addressList = rows;
						} else if (type === 'pullDown') {
							this.addressList = rows;
							uni.stopPullDownRefresh();
							this.showLoadMore = false;
							this.loadMoreText = '加载中...';
						} else if (type === 'reachBottom') {
							if (rows.length > 0) {
								this.addressList = this.addressList.concat(rows);
								this.loadMoreText = '加载更多';
							} else {
								this.loadMoreText = '已加载全部';
							}
						}
					} else {
						showInfoToast(res.data.message);
					}
					uni.hideLoading()
				}).catch(err => {
					console.log(err);
				})
			},
			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().selectedAddress = item;
					this.$api.prePage().computeExpressFee()
					uni.navigateBack()
				}
			},
			// 新增地址
			addAddress(type, item) {
				let isDefault = 0
				if ((type === 'add' && this.addressList.length === 0) || (type === 'edit' && this.addressList.length === 1)) {
					isDefault = 1
				}
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}&isDefault=${isDefault}`
				})
			},
			//添加或修改成功之后回调
			refreshList() {
				this.loadAddressList('init')
			}
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120upx;
	}

	.content {
		position: relative;
	}

	.list {
		display: flex;
		align-items: center;
		padding: 20upx 30upx;
		background: #fff;
		position: relative;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
		margin-right: 10upx;
	}

	.address-box {
		display: flex;
		align-items: center;
		
		.address {
			margin-top: 10upx;
			font-size: 30upx;
			color: $font-color-light;
		}
	}

	.u-box {
		font-size: 28upx;
		color: $font-color-light;
		margin-top: 10upx;

		.name {
			color: $font-color-dark;
			margin-right: 30upx;
		}
		
		.tag {
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 40upx;
		color: $font-color-light;
		padding-left: 30upx;
	}

	.zy-add-btn {
		position: fixed;
		left: 5%;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 90%;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);
	}
</style>
